【问题标题】:Audio is not playing after stop serviec in Media player在媒体播放器中停止服务后不播放音频
【发布时间】:2013-05-27 19:12:15
【问题描述】:

伙计们,我正在我的应用程序中处理音频模块,所以我创建了带有服务的简单音频演示。

它工作到Play - Pause - Playing in Background 但问题很奇怪。

只是在启动应用程序时,我添加了两个类似的按钮

  • Play/Pause(会根据状态改变标题)

  • stop(停止服务)

现在正如我所说,我的 play-pause & Stop 工作正常,但是当我在点击 stop 后尝试再次播放音频时,它会导致 Start 出现问题

Error : 05-01 13:37:42.671: E/start(8096): java.lang.IllegalStateException

Audio_Activity.java

public class Audio_Activity extends Activity {
  
    private static final String TAG = "ServicesDemo";
    public static Button play_pause;
    public static Button buttonStop;
    Audio_Service ms;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ms = new Audio_Service();
        play_pause = (Button) findViewById(R.id.play_pause);
        buttonStop = (Button) findViewById(R.id.buttonStop);

        play_pause.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (isMyServiceRunning()) {
                        ms.play_pause();
                    } else {
                        startService(new Intent(Audio_Activity.this,
                                Audio_Service.class));
                        play_pause.setText("Pause");
                    }
                }
            });

        buttonStop.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (isMyServiceRunning()) {
                        ms.stop_audio();
                        stopService(new Intent(Audio_Activity.this,
                                Audio_Service.class));
                        play_pause.setText("Play");
                    } else {
                        stopService(new Intent(Audio_Activity.this,
                                Audio_Service.class));
                        play_pause.setText("Play");
                    }
                }

            });

    }

    public boolean isMyServiceRunning() {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningServiceInfo service: manager
            .getRunningServices(Integer.MAX_VALUE)) {
            if (Audio_Service.class.getName().equals(
                    service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }
}

Audio_Service.java

public class Audio_Service extends Service implements OnErrorListener,
OnCompletionListener, OnPreparedListener {

    public static MediaPlayer player = new MediaPlayer();
    Audio_Activity ac = new Audio_Activity();

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {

        Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onDestroy() {
        try {
            Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG)
                .show();
        } catch (Exception e) {

            Log.e("ONdistroy", "" + e);
        }
    }

    @Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();

        try {
            Log.e("player_status", "" + player);
            String url = "MY_WEB_URL";
            player.setAudioStreamType(AudioManager.STREAM_MUSIC);
            player.reset();
            player.setDataSource(url);
            player.prepareAsync();
            player.setOnPreparedListener(this);
            player.setOnErrorListener(this);
            player.setOnCompletionListener(this);

        } catch (Exception e) {
            // TODO: handle exception
            Log.e("start", "" + e);
        }
        // player.start();
    }

    public void play_pause() {
        try {
            if (player != null) {
                if (player.isPlaying()) {
                    ac.play_pause.setText("Play");
                    player.pause();
                } else {
                    ac.play_pause.setText("Pause");
                    player.start();

                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            Log.e("play_pause", "" + e);
        }
    }

    public void stop_audio() {
        try {
            if (player != null) {
                if (player.isPlaying()) {
                    ac.play_pause.setText("Play");
                    player.stop();
                    player.release();

                } else {
                    ac.play_pause.setText("Play");
                    player.release();

                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            Log.e("stop_service", "" + e);
        }
    }

    public void cleanUp() {

        if (player != null) {
            player.stop();
            player = null;
            player.release();
        }

    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        // TODO Auto-generated method stub
        try {
            Log.d("complete", "called...........");

            player.stop();
            player.release();
            player = null;
            stopService(new Intent(this, Audio_Service.class));
            ac.play_pause.setText("Play");

        } catch (Exception e) {
            // TODO: handle exception
            Log.e("complete", "" + e);
        }

    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        // TODO Auto-generated method stub
        try {
            player.stop();
            player.release();
            player = null;

            // mp.reset();
            ac.play_pause.setText("Play");
        } catch (Exception e) {
            // TODO: handle exception
            Log.e("onError", "" + e);
        }

        return false;
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        // TODO Auto-generated method stub
        try {
            player.start();
        } catch (Exception e) {
            // TODO: handle exception
            Log.e("onPrepare", "" + e);
        }
    }

}

我在Manifestfile 中添加了以下内容

<uses-permission android:name="android.permission.INTERNET"/>
<service 
 android:name=".Audio_Service" 
 android:enabled="true" />

更新

我知道我可以在理想状态下播放音频,因为我必须设置 reset(); 但不知道在哪里。因为如果我设置为 onDestroy;cleanUp()stop_audio()onStart() 然后它又增加了一个错误,那就是NullpointerException

请大家帮帮我。我最近两天收到了这个错误。 如果我错了,请纠正我。

感谢您耐心解答我的问题。

【问题讨论】:

    标签: android service media-player


    【解决方案1】:

    您应该使用 startService() 来运行您的服务并使用 Binder 与其通信。它不是一个常规对象,您需要让 Android 操作系统了解它,并且您需要按照它的预期行事。

    http://developer.android.com/reference/android/app/Service.html

    你不应该自己创建一个实例,也不应该直接调用它的方法。

    【讨论】:

    • 我在play_pause点击事件中调用了我的服务startService(new Intent(Audio_Activity.this,Audio_Service.class));
    • 但是您自己使用 new 创建了一个实例,并使用常规方法调用与该实例通信。这不是服务的使用方式。在调用 startService() 后,您需要让操作系统自己创建实例。您需要等待建立与服务的连接,并且需要使用 Binder 与其通信。有关详细信息,请参阅链接。
    • 感谢您的链接。很好。但我解决了我的问题,我必须在stop_audio() 中添加player = null;
    • 这是一个复杂的问题。 Android服务不应该这样使用,而且我对Android的服务代码也不是很熟悉,所以不知道这样使用会有什么效果。
    • ,好的。顺便说一句,现在我以前的代码正在运行,现在我也按照你的链接实现了。所以我认为这应该是击球手,因为那是开发人员的链接。谢谢你的帮助。
    【解决方案2】:

    我在 5 分钟后得到了解决方案,但我犯了一个愚蠢的错误。我忘记添加 player = null;

    整个代码运行良好,没有任何错误,但其中一位开发人员(销售商)也建议我 Refer this。我也采用了这种方式,而且效果也很好。

    public void stop_audio() {
         try {
             if (player != null) {
                 if (player.isPlaying()) {
                     ac.play_pause.setText("Play");
                     player.stop();
                     player.release();
                     player = null; // forgot this statement  
    
                 } else {
                     ac.play_pause.setText("Play");
                     player.release();
                     player = null; // forgot this statement  
    
                 }
             }
         } catch (Exception e) {
             // TODO: handle exception
             Log.e("stop_service", "" + e);
         }
     }
    

    【讨论】:

      【解决方案3】:

      您尝试在播放音频后停止

      try {
      
          player.reset();
          player.setDataSource(url);
          player.prepare();
          player.seekTo(0);
      
      } catch (IllegalArgumentException e1) {
          e1.printStackTrace();
      } catch (SecurityException e1) {
          e1.printStackTrace();
      } catch (IllegalStateException e1) {
          e1.printStackTrace();
      } catch (IOException e1) {
          e1.printStackTrace();
      }
      player.start();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多