声音无法播放

时间:2018-10-05 06:13:27

标签: android

嗨,我是新来的,最近才刚开始。

我对在创建上播放声音有疑问。

我将数据放到res / raw /上,名称是smile,它是mp3。

下面是我的代码。

package com.arnchr.sounddemo;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MediaPlayer mplayer = MediaPlayer.create(this, R.raw.laugh);

    mplayer.start();

}

我尝试在android studio中播放它,但我不断收到此错误消息。

    10-06 18:52:41.677 12916-12978/system_process E/AudioTrack: Could not get audio output for stream type 1, usage 0, sample rate 48000, format 0x1, channel mask 0x3, flags 0x4

    --------- beginning of system
10-06 18:52:41.685 12916-12978/system_process E/SoundPool: Error creating AudioTrack
10-06 18:52:41.791 20549-20549/com.arnchr.sounddemo E/MediaPlayer: Should have subtitle controller already set
10-06 18:52:41.808 20549-20549/com.arnchr.sounddemo E/MediaPlayer: Should have subtitle controller already set
10-06 18:52:41.833 12635-20775/? E/NuPlayerDecoder: Failed to open AudioSink on format change for OMX.google.mp3.decoder (err=-19)
10-06 18:52:41.834 12635-20772/? E/NuPlayer: received error(0xffffffed) from audio decoder, flushing(0), now shutting down
10-06 18:52:41.835 20549-20564/com.arnchr.sounddemo E/MediaPlayer: error (1, -19)
10-06 18:52:42.135 20549-20549/com.arnchr.sounddemo E/MediaPlayer: Error (1,-19)
10-06 18:56:57.220 20806-20806/? E/memtrack: Couldn't load memtrack module (No such file or directory)
10-06 18:56:57.220 20806-20806/? E/android.os.Debug: failed to load memtrack module: -2
10-06 18:57:02.308 20824-20824/? E/memtrack: Couldn't load memtrack module (No such file or directory)
10-06 18:57:02.308 20824-20824/? E/android.os.Debug: failed to load memtrack module: -2

我不明白此错误消息。

1 个答案:

答案 0 :(得分:1)

您可以尝试以下操作:

9.9.0-1

您也可以这样做:

package com.arnchr.sounddemo;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MediaPlayer player = new MediaPlayer();
    player.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        String url = ""; // your URL here
        player.setDataSource(url);
        player.prepareAsync();
        /*Generally prepareAsync is used when you load resources from the web, alternatively you can use prepare() if the resource is local*/
     } catch (IOException e) {
        Log.e(TAG, "error loading resources for player");
        e.printStackTrace();
    }

    player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer player) {
        player.start();
    }
}