Java 如何从我的应用程序共享音频文件
因此,我正在尝试从操作栏共享图标共享我的应用程序为声音云创建的音频。当我录制完音频后,我单击图标,声音云确实会作为一个选项出现,但当我单击它时,它会转到声音云,然后立即返回到我的应用程序,根本不共享 这是我开始保存过程和共享过程的记录活动Java 如何从我的应用程序共享音频文件,java,android,eclipse,Java,Android,Eclipse,因此,我正在尝试从操作栏共享图标共享我的应用程序为声音云创建的音频。当我录制完音频后,我单击图标,声音云确实会作为一个选项出现,但当我单击它时,它会转到声音云,然后立即返回到我的应用程序,根本不共享 这是我开始保存过程和共享过程的记录活动 //convert inputstream to byte array public static byte[] getBytesFromInputStream(InputStream is) { ByteArrayOutputStream os =
//convert inputstream to byte array
public static byte[] getBytesFromInputStream(InputStream is)
{
ByteArrayOutputStream os = new ByteArrayOutputStream();
try
{
byte[] buffer = new byte[0xFFFF];
for (int len; (len = is.read(buffer)) != -1;)
os.write(buffer, 0, len);
os.flush();
return os.toByteArray();
}
catch (IOException e)
{
return null;
}
}
public void saveInputStream(InputStream is) throws IOException
{
int n = 0;
DataInputStream in1;
in1 = new DataInputStream(is);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try
{
while ((n = in1.read()) != -1)
{
bos.write(n);
}
}
catch (IOException e)
{
e.printStackTrace();
}
ByteBuffer bb = ByteBuffer.wrap(bos.toByteArray());
bb.order(ByteOrder.LITTLE_ENDIAN);
ShortBuffer sb = bb.asShortBuffer();
for (int i = 0; i < sb.capacity(); i++) {
beatsShortList.add(sb.get(i));
}
}
//add zeros to shorter audio
public void completeStreams(List<Short> l1,List<Short> l2)
{
if(l1.size() > l2.size())
{
while(l1.size() != l2.size())
{
l2.add((short)0);
}
}
if(l2.size() > l1.size())
{
while(l1.size() != l2.size())
{
l1.add((short)0);
}
}
}
//converts short list to short array
public short[] buildShortArray(List<Short> list)
{
short[] arr = new short[list.size()];
for(int i = 0; i < list.size(); i++)
{
arr[i] = list.get(i);
}
return arr;
}
//overriding back button functionality
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.d(this.getClass().getName(), "back button pressed");
if(player != null)
player.stop();
play_thread_running = false;
//beat_playing_thread.stop();
//lyrics_playing_thread.stop();
return super.onKeyDown(keyCode, event);
}
return true;
}
public void showDialog()
{
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("What's Your Song Name!");
alert.setMessage("Title");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
mixed_file_name = input.getText().toString();
//progress = ProgressDialog.show(getApplicationContext(), "", "Mixing and Saving...");
mix_and_save_thread = new Thread(new Runnable() {
@Override
public void run() {
try
{
Log.d("Check:","Line=> 437");
byte[] mixed_audio = mixSound();
Log.d("Check:","Line=> 439");
String fileName = getFilename();
FileOutputStream fout = new FileOutputStream(fileName);
long byteRate = RECORDER_BPP * RECORDER_SAMPLERATE * channels/8;
WriteWaveFileHeader(fout, mixed_audio.length+40, mixed_audio.length, RECORDER_SAMPLERATE, channels, byteRate);
Log.d("Check:","Line=> 444");
fout.write(mixed_audio);
Log.d("Check:","Line=> 446");
fout.close();
//progress.dismiss();
}
catch (IOException e)
{
e.printStackTrace();
}
}
});
mix_and_save_thread.start();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* Menu item selected handler that shares the selected post
*
* @param item - menu item clicked
* @retrun boolean
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.action_sharing) { sharePost(); }
return super.onOptionsItemSelected(item);
}
private void sharePost() {
File audio = new File("/meip/to/audio.wav");
Intent intent = new Intent(Intent.ACTION_SEND).setType("audio/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(audio));
startActivity(Intent.createChooser(intent, "Share to"));
}
}
//将inputstream转换为字节数组
公共静态字节[]getBytesFromInputStream(InputStream为)
{
ByteArrayOutputStream os=新建ByteArrayOutputStream();
尝试
{
字节[]缓冲区=新字节[0xFFFF];
for(int len;(len=is.read(buffer))!=-1;)
写操作(缓冲区,0,len);
os.flush();
返回os.toByteArray();
}
捕获(IOE异常)
{
返回null;
}
}
public void saveInputStream(InputStream为)引发IOException
{
int n=0;
数据输入流in1;
in1=新的数据输入流(is);
ByteArrayOutputStream bos=新建ByteArrayOutputStream();
尝试
{
而((n=in1.read())!=-1)
{
bos.write(n);
}
}
捕获(IOE异常)
{
e、 printStackTrace();
}
ByteBuffer bb=ByteBuffer.wrap(bos.toByteArray());
bb.order(ByteOrder.LITTLE_ENDIAN);
ShortBuffer sb=bb.asShortBuffer();
对于(int i=0;il2.size())
{
而(l1.size()!=l2.size())
{
l2.添加((短)0);
}
}
如果(l2.size()>l1.size())
{
而(l1.size()!=l2.size())
{
l1.添加((短)0);
}
}
}
//将短列表转换为短数组
公共短[]buildShortArray(列表)
{
short[]arr=新的short[list.size()];
对于(int i=0;i437”);
字节[]mixed_audio=mixSound();
Log.d(“检查:”,“行=>439”);
字符串fileName=getFilename();
FileOutputStream fout=新的FileOutputStream(文件名);
long byteRate=记录器\ BPP*记录器\采样器*通道/8;
WriteWaveFileHeader(fout,混合音频.length+40,混合音频.length,记录器\u采样器,频道,字节);
Log.d(“检查:”,“行=>444”);
四写(混音);
Log.d(“检查:”,“行=>446”);
fout.close();
//进步。解散();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
}
});
混合和保存线程。开始();
}
});
alert.setNegativeButton(“取消”,新建DialogInterface.OnClickListener()){
public void onClick(对话框接口对话框,int whichButton){
//取消了。
}
});
alert.show();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
/**
*菜单项“共享选定帖子的选定处理程序”
*
*@param item-已单击菜单项
*@retrun布尔值
*/
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
int itemId=item.getItemId();
如果(itemId==R.id.action_sharing){sharePost();}
返回super.onOptionsItemSelected(项目);
}
私有void sharePost(){
File audio=新文件(“/meip/to/audio.wav”);
意图=新意图(Intent.ACTION\u SEND).setType(“音频/*”);
intent.putExtra(intent.EXTRA_流,Uri.fromFile(音频));
startActivity(Intent.createChooser(Intent,“共享给”);
}
}
是否检查音频是否为空?你在logcat中有任何错误吗?是的,我检查了音乐是否正常,我播放了音乐并保存在手机上,但当我打开共享并单击声音云时,它会立即打开并返回到我的应用程序。