如何从我的应用程序共享音频文件

时间:2014-06-08 18:22:00

标签: java android eclipse

因此,我尝试将操作栏共享图标中的应用创建的音频分享到声音云。当我完成录制音频时,我点击图标,声音云确实会出现,但是当我点击它时,它会进入声音云,然后立即回到我的应用程序而不是共享。

以下是我开始保存过程和共享过程的记录活动

//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"));
    }
}

0 个答案:

没有答案