【问题标题】:Android media player doesn't workAndroid 媒体播放器无法正常工作
【发布时间】:2015-03-13 04:49:26
【问题描述】:

我正在尝试播放警报声,但我收到了这个错误并且我听不到声音。它不会导致应用程序出现任何问题或崩溃,但没有声音。

03-13 00:35:01.138: D/MediaPlayer(2580): Couldn't open file on client side, trying server side
03-13 00:35:01.138: E/MediaPlayer(2580): Unable to create media player
03-13 00:35:01.138: I/alarmReceiver(2580): No audio file founded!

根据我的调试,这个错误发生在这行代码运行之前:

mMediaPlayer.setDataSource(context, alert);

这不仅发生在模拟器中,也发生在真实设备中;我什么都听不见。

这是我的代码:

playSound(this, getAlarmUri()); 

private void playSound(Context context, Uri alert) {        
    mMediaPlayer = new MediaPlayer();       
    try {           
        mMediaPlayer.reset();
        **mMediaPlayer.setDataSource(context, alert);**
        final AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

        Log.i("getStreamVolume", am.getStreamVolume(AudioManager.STREAM_ALARM)+"");

        if (am.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
        }
    } catch (Exception e) {
        // TODO: handle exception
        Log.i("alarmReceiver", "No audio file founded!");
    }
}

private Uri getAlarmUri()
    {       
        Uri alert=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        if(alert==null)
        {
            alert= RingtoneManager.getDefaultUri((RingtoneManager.TYPE_NOTIFICATION));
            if(alert==null)
            {
                alert=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
            }
        }
        Log.i("alert", alert.toString());
        return alert;
    }

【问题讨论】:

    标签: java android stream android-mediaplayer


    【解决方案1】:

    在重置媒体播放器之前尝试设置数据源。
    ... mMediaPlayer = new MediaPlayer(); mMediaPlayer.setDataSource(context, alert); try { mMediaPlayer.reset(); ...

    【讨论】:

    • 非常感谢。但它不起作用:(。实际上,我之前没有重置,因为我读了一篇文章,所以我把它放在那里,它对你有用吗?还有其他想法吗?谢谢
    • 您还有什么建议吗?还有谁?谢谢
    • 对不起,我还没有对媒体播放器做太多,但是我上次遇到问题时给出的建议对我有用。很遗憾,我没有更多建议了。
    【解决方案2】:

    好的,伙计们,我找到了解决问题的方法。

    我在 res 文件夹下创建了 raw 文件夹并将我的 song.mp3 放在那里

    mMediaPlayer = new MediaPlayer(); 
        try {
            final AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            mMediaPlayer = MediaPlayer.create(context, R.raw.song);
            mMediaPlayer.start();
    
    
        } catch (IllegalArgumentException e) {
    
            // TODO Auto-generated catch block
    
            e.printStackTrace();
    
        } catch (SecurityException e) {
    
            // TODO Auto-generated catch block
    
            e.printStackTrace();
    
        } catch (IllegalStateException e) {
    
            // TODO Auto-generated catch block
    
            e.printStackTrace();
    
        }
    

    完成后不要忘记停止并释放媒体播放器。

    mMediaPlayer.stop();
    mMediaPlayer.release();
    

    【讨论】:

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