在声音Android和GT不打;冰淇淋三明治明治、不打、冰淇淋、声音

2023-09-07 14:20:47 作者:屌丝男士^.^

我用下面code播放声音。一切ICS之前正常工作。但在ICS和更高版本听不到声音。没有错误,但没有发出任何声音。

编辑:请注意,下面的code是由broadcase接收器触发。广播接收器调用异步任务。在asycn任务的后处理方法,下面的方法被调用。

还有什么错误可能是?

 公共静态无效playSound(最终上下文的背景下,最终诠释体积,            开放的我们的uri,最终诠释流,诠释MAXTIME,诠释滚动时间){        //停止播放();        / *        如果(流℃,||流> 100){            抛出新抛出:IllegalArgumentException(                    卷必须是0和100 .Current体积之间                            +音量);        } * /        最后AudioManager mAudioManager =(AudioManager)上下文                .getSystemService(Context.AUDIO_SERVICE);        INT deviceLocalVolume = getDeviceVolume(音量,                mAudioManager.getStreamMaxVolume(流));        Log.d(TAG,                设备最大音量=                        + mAudioManager.getStreamMaxVolume(流)                        +的流类型+流);        Log.d(TAG,播放声音+ uri.toString()                +与设备本地卷+ deviceLocalVolume);        最终诠释oldVolume = mAudioManager.getStreamVolume(流);        //设置音量什么,我们希望它是。在这种情况下,它的最大体积        //为报警流。        Log.d(Constants.APP_TAG设置设备本地卷为+ deviceLocalVolume);        mAudioManager.setStreamVolume(流deviceLocalVolume,                AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);         最终的MediaPlayer媒体播放器=新的MediaPlayer();         golbalMMediaPlayer = MediaPlayer的;        尝试{             最终在preparedListener在preparedListener =新在preparedListener(){                @覆盖                在prepared公共无效(最终的MediaPlayer MP){                    Log.d(TAGonMediaPlayercompletion监听器);                    mp.start();                    countDownTimer.start();                }            };            mediaPlayer.setDataSource(context.getApplicationContext(),URI);            mediaPlayer.setAudioStreamType(流);            mediaPlayer.setLooping(假);            mediaPlayer.setOn preparedListener(在preparedListener);            mediaPlayer.setOnCompletionListener(新OnCompletionListener(){                @覆盖                公共无效onCompletion(MediaPlayer的MP){                    Log.d(Constants.APP_TAG,进入媒体播放器的onCompletion监听器);                    mAudioManager.setStreamVolume(流oldVolume,                            AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);                    尝试{                    如果(媒体播放器= NULL&放大器;!&安培; mediaPlayer.isPlaying()){                        mediaPlayer.release();                    }                    }赶上(例外前){                        Log.e(Constants.APP_TAG,关于oncompletion听众错误,前);                    }                }            });             CountDownTimer定时器=新CountDownTimer(MAXTIME * 1000,滚动时间* 1000){                @覆盖                公共无效onTick(长millisUntilFinished){                    Log.d(TAG,嘀在播放声音);                }                @覆盖                公共无效onFinish(){                    Log.d(TAG,计时器结束);                    停止播放();                }            };            countDownTimer =定时器;            。媒体播放器prepareAsync();        }赶上(例外五){            Log.e(TAG的问题,同时播放声音,E);        } {最后        }    } 

日志:

 :07-01 00:00:00.030:D / beephourly(9500):为流类型5设备最大音量= 707-01 00:00:00.030:D / beephourly(9500):播放声音的内容:// /内部/音频/媒体/ 166设备本地卷707-01 00:00:00.030:D / beephourly(9500):设置装置局部体积至707-01 00:00:00.080:D / beephourly(9500):用振动模式= [J @ 428bae2007-01 00:00:00.090:D / beephourly(9500):将显示正常通知07-01 00:00:00.100:D / beephourly(9500):通知被启用07-01 00:00:00.100:D / usersettings(9500):HR = 007-01 00:00:00.110:D / beephourly(9500):onMediaPlayercompletion听者07-01 00:00:00.451:D / beephourly(9500):蜱在播放声音07-01 00:00:20.460:D / beephourly(9500):定时完成07-01 00:00:20.460:D / beephourly(9500):有要求停止播放07-01 00:00:20.460:D / beephourly(9500):取消countdowntimer07-01 00:00:20.460:D / beephourly(9500):释放现在的MediaPlayer 

解决方案

试试这个:

播放声音

 公共类PlaySound扩展活动实现OnTouchListener {  私人的Soundpool的Soundpool;  私人诠释soundID;  布尔加载= FALSE;/ **当第一次创建活动调用。 * /@覆盖公共无效的onCreate(捆绑savedInstanceState){  super.onCreate(savedInstanceState);  的setContentView(R.layout.main);  查看查看= findViewById(R.id.textView1);  view.setOnTouchListener(本);  //设置硬件按钮来控制音乐  this.setVolumeControlStream(AudioManager.STREAM_MUSIC);  //加载声音  的Soundpool =新的Soundpool(10,AudioManager.STREAM_MUSIC,0);  soundPool.setOnLoadCompleteListener(新OnLoadCompleteListener(){  @覆盖  公共无效的onLoadComplete(的Soundpool的Soundpool,INT sampleId,      INT状态){     加载=真实的;  }  });  soundID = soundPool.load(在此,R.raw.sound1,1);  } @覆盖 公共布尔onTouch(视图V,MotionEvent事件){  如果(event.getAction()== MotionEvent.ACTION_DOWN){  //获取用户的声音设置  AudioManager audioManager =(AudioManager)getSystemService(AUDIO_SERVICE);  浮动actualVolume =(浮点)audioManager      .getStreamVolume(AudioManager.STREAM_MUSIC);  浮动maxVolume =(浮点)audioManager      .getStreamMaxVolume(AudioManager.STREAM_MUSIC);  浮动量= actualVolume / maxVolume;  //声音是装了吗?  如果(装入){    soundPool.play(soundID,体积,容量,1,0,1F);    Log.e(测试,播放声音);  }} 返回false;}} 
有着逆天美貌和无敌口感的冰淇淋三明治,你会做吗

的布局文件:的

 <?XML版本=1.0编码=UTF-8&GT?;< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android机器人:layout_width =match_parent机器人:layout_height =match_parent机器人:方向=垂直><的TextView    机器人:ID =@ + ID / textView1    机器人:layout_width =match_parent    机器人:layout_height =match_parent    机器人:文字=点击屏幕上开始播放>< / TextView的>< / LinearLayout中> 

来源链接:http://www.vogella.com/tutorials/AndroidMedia/article.html#sound

I was using following code to play sound. Everything worked fine before ICS. But on ICS and higher versions no sound is heard. There is no error, but no sound can be heard.

EDIT: Note, the following code is triggered by a broadcase receiver. BroadCast receiver invokes a async task. In the post process method of asycn task the following method is called.

What could the error possibly be?

public static void playSound(final Context context, final int volume,
            Uri uri, final int stream, int maxTime, int tickTime) {
        //stopPlaying();
        /*
        if (stream < 0 || stream > 100) {
            throw new IllegalArgumentException(
                    "volume must be between 0 and 100 .Current volume "
                            + volume);
        }*/

        final AudioManager mAudioManager = (AudioManager) context
                .getSystemService(Context.AUDIO_SERVICE);

        int deviceLocalVolume = getDeviceVolume(volume,
                mAudioManager.getStreamMaxVolume(stream));

        Log.d(TAG,
                "device max volume = "
                        + mAudioManager.getStreamMaxVolume(stream)
                        + " for streamType " + stream);
        Log.d(TAG, "playing sound " + uri.toString()
                + " with device local volume " + deviceLocalVolume);

        final int oldVolume = mAudioManager.getStreamVolume(stream);

        // set the volume to what we want it to be. In this case it's max volume
        // for the alarm stream.
        Log.d(Constants.APP_TAG, "setting device local volume to " + deviceLocalVolume);
        mAudioManager.setStreamVolume(stream, deviceLocalVolume,
                AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);

         final MediaPlayer mediaPlayer = new MediaPlayer();
         golbalMMediaPlayer = mediaPlayer;

        try {
             final OnPreparedListener OnPreparedListener = new OnPreparedListener() {

                @Override
                public void onPrepared(final MediaPlayer mp) {
                    Log.d(TAG, "onMediaPlayercompletion listener");
                    mp.start();
                    countDownTimer.start();
                }
            };

            mediaPlayer.setDataSource(context.getApplicationContext(), uri);
            mediaPlayer.setAudioStreamType(stream);
            mediaPlayer.setLooping(false);
            mediaPlayer.setOnPreparedListener(OnPreparedListener);
            mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    Log.d(Constants.APP_TAG, "Entered onCompletion listener of mediaplayer");
                    mAudioManager.setStreamVolume(stream, oldVolume,
                            AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
                    try{
                    if(mediaPlayer != null && mediaPlayer.isPlaying()){
                        mediaPlayer.release();
                    }
                    }catch(Exception ex){
                        Log.e(Constants.APP_TAG, "error on oncompletion listener" ,ex);
                    }
                }

            });

             CountDownTimer timer = new CountDownTimer(maxTime*1000, tickTime*1000) {

                @Override
                public void onTick(long millisUntilFinished) {
                    Log.d(TAG, "tick while playing sound ");
                }

                @Override
                public void onFinish() {
                    Log.d(TAG, "timer finished");
                    stopPlaying();
                }
            };

            countDownTimer = timer;

            mediaPlayer.prepareAsync();

        } catch (Exception e) {
            Log.e(TAG, "problem while playing sound", e);
        } finally {

        }
    }

LOGS:

:07-01 00:00:00.030: D/beephourly(9500): device max volume = 7 for streamType 5
07-01 00:00:00.030: D/beephourly(9500): playing sound content://media/internal/audio/media/166 with device local volume 7
07-01 00:00:00.030: D/beephourly(9500): setting device local volume to 7
07-01 00:00:00.080: D/beephourly(9500): vibrating with pattern = [J@428bae20
07-01 00:00:00.090: D/beephourly(9500): will show normal notification
07-01 00:00:00.100: D/beephourly(9500): notification is enabled
07-01 00:00:00.100: D/usersettings(9500): hr = 0
07-01 00:00:00.110: D/beephourly(9500): onMediaPlayercompletion listener
07-01 00:00:00.451: D/beephourly(9500): tick while playing sound 
07-01 00:00:20.460: D/beephourly(9500): timer finished
07-01 00:00:20.460: D/beephourly(9500): got request to stop playing
07-01 00:00:20.460: D/beephourly(9500): cancelling countdowntimer
07-01 00:00:20.460: D/beephourly(9500): releasing mediaplayer now

解决方案

Try this :

Playing sound

public class PlaySound extends Activity implements OnTouchListener {
  private SoundPool soundPool;
  private int soundID;
  boolean loaded = false;


/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  View view = findViewById(R.id.textView1);
  view.setOnTouchListener(this);
  // Set the hardware buttons to control the music
  this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
  // Load the sound
  soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
  soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {

  @Override
  public void onLoadComplete(SoundPool soundPool, int sampleId,
      int status) {
     loaded = true;
  }
  });
  soundID = soundPool.load(this, R.raw.sound1, 1);

  }

 @Override
 public boolean onTouch(View v, MotionEvent event) {
  if (event.getAction() == MotionEvent.ACTION_DOWN) {
  // Getting the user sound settings
  AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
  float actualVolume = (float) audioManager
      .getStreamVolume(AudioManager.STREAM_MUSIC);
  float maxVolume = (float) audioManager
      .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
  float volume = actualVolume / maxVolume;
  // Is the sound loaded already?
  if (loaded) {
    soundPool.play(soundID, volume, volume, 1, 0, 1f);
    Log.e("Test", "Played sound");
  }
}
 return false;
}
} 

Layout file :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Click on the screen to start playing" >
</TextView>

</LinearLayout> 

Source link : http://www.vogella.com/tutorials/AndroidMedia/article.html#sound