gpt4 book ai didi

android - 声音没有在 Create 上播放

转载 作者:行者123 更新时间:2023-11-29 23:31:03 25 4
gpt4 key购买 nike

您好,我是这方面的新手,最近才开始。

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

我把我的数据放在 res/raw/上,名字是 laugh,它是 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

我不明白这个错误信息。

最佳答案

您可以尝试如下操作:

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();
}
}

你也可以这样做:

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 = MediaPlayer.create(this, R.raw.laugh);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
player.prepare();
} 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();
}
}

关于android - 声音没有在 Create 上播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52659328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com